CSharpTest.Net
GetOrAdd(TKey,Converter<TKey,TValue>) Method
See Also  Example Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.Collections Namespace > LurchTable<TKey,TValue> Class > GetOrAdd Method : GetOrAdd(TKey,Converter<TKey,TValue>) Method

key
The key of the element to add.
fnCreate
Constructs a new value for the key.

Glossary Item Box

Adds a key/value pair to the IDictionary if the key does not already exist.

Syntax

Visual Basic (Declaration) 
Public Overloads Function GetOrAdd( _
   ByVal key As TKey, _
   ByVal fnCreate As Converter(Of TKey,TValue) _
) As TValue
C# 
public TValue GetOrAdd( 
   TKey key,
   Converter<TKey,TValue> fnCreate
)

Parameters

key
The key of the element to add.
fnCreate
Constructs a new value for the key.

Example

Library/Library.Test/TestLurchTable.cs

C#Copy Code
var data = new LurchTableTest<int, string>();
Assert.AreEqual("a", data.GetOrAdd(1, "a"));
Assert.AreEqual("a", data.GetOrAdd(1, "b"));

Assert.AreEqual("b", data.GetOrAdd(2, k => "b"));
Assert.AreEqual("b", data.GetOrAdd(2, k => "c"));
VB.NETCopy Code
Dim data As var = New LurchTableTest(Of Integer, String)()
Assert.AreEqual("a", data.GetOrAdd(1, "a"))
Assert.AreEqual("a", data.GetOrAdd(1, "b"))

Assert.AreEqual("b", data.GetOrAdd(2, Function(k) "b"))
Assert.AreEqual("b", data.GetOrAdd(2, Function(k) "c"))

Requirements

Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7

See Also

Generated with Document! X 2011 by Innovasys